package org.indexador.util.stax;

import java.io.*;

import javax.xml.stream.*;
import javax.xml.stream.events.*;

public class ContadorRegistrosWikipediaStaX {
  private long inicio = System.currentTimeMillis();

  public void leDados(String configFile) {
    int total = 0;
    try {
      XMLInputFactory inputFactory = XMLInputFactory.newInstance();
      InputStream in = new FileInputStream(configFile);
      XMLEventReader eventReader = inputFactory.createXMLEventReader(in);
      while (eventReader.hasNext()) {
        XMLEvent event = eventReader.nextEvent();
        if (event.isStartElement()
            && event.asStartElement().getName().getLocalPart().equals("page")) {
          total++;
          if (total % 100000 == 0) System.out.println("Parcial: " + total);
        }
      }
      System.out.println("Total: " + total);
      System.out.println("Tempo total Stax: "
          + (System.currentTimeMillis() - inicio) / 1000); //1489505
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public static void main(String[] args) {
    //    String caminho = "/Users/marcoreis/Downloads/menu_sispl.xml";
    String caminho = "/Users/marcoreis/Downloads/ptwiki-20110714-pages-articles.xml";
    new ContadorRegistrosWikipediaStaX().leDados(caminho);
  }
}